<?php
/**
 * Created by PhpStorm.
 * User: kxy
 * Date: 2019/1/18
 * Time: 上午12:20
 */

namespace App\Http\Controllers\Web\Baike;

use App\Http\Controllers\Controller;
use App\Models\Encyclopedia\Article;
use App\Models\Encyclopedia\Category;

class EncyclopediasController extends Controller
{
    public function index()
    {
        $banner = $this->banner();
        $top = $this->top();
        $design = $this->design();
        $construction = $this->construction();
        $sketch = $this->sketch();
        $category = Category::all();
        $material = $this->material();
        $jiaju = $this->article('jiaju');
        $jiadian = $this->article('weixiu');
        $ruanzhuang = $this->article('ruanshi');
        $fengshui = $this->article('fengshui');
        $xinwen = $this->article('xinwen');
        $zatan = $this->article('zatan');
        return view('baike.index', compact(
            'banner', 'top', 'design', 'construction', 'sketch', 'category', 'material',
            'jiaju', 'jiadian', 'ruanzhuang', 'fengshui', 'xinwen', 'zatan'
        ));
    }

    public function column($code)
    {
        $category = Category::all();
        $categoryOne = $category->where('code', $code)->first();
        $articles = Article::where('category_id', $categoryOne->id ?? '')->with('media')->paginate(15);
        $recommend = $this->recommend();
        return view('baike.column', compact('category', 'articles', 'categoryOne', 'recommend'));
    }

    public function banner()
    {
        $banner = Article::inRandomOrder()->take(5)->get();
        $banner = $banner->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });
        return $banner;
    }

    protected function top()
    {
        $top = Article::inRandomOrder()->take(6)->get();
        $top = $top->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });
        return $top;
    }

    protected function design()
    {
        $category = Category::whereCode('sheji')->first();
        $design = Article::design($category->id ?? '')->take(6)->get();
        $design = $design->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });
        return $design;
    }

    protected function construction()
    {
        $category = Category::whereCode('shigong')->first();
        $construction = Article::design($category->id ?? '')->take(6)->get();
        $construction = $construction->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });
        return $construction;
    }

    protected function sketch()
    {
        $sketch = Article::inRandomOrder()->take(6)->get();
        $sketch = $sketch->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });
        return $sketch;
    }

    protected function recommend()
    {
        $recommend = Article::inRandomOrder()->take(6)->get();
        $recommend = $recommend->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });
        return $recommend;
    }

    protected function material()
    {
        $category = Category::whereCode('jiancai')->first();
        $material = Article::design($category->id ?? '')->take(6)->get();
        $material = $material->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });
        return $material;
    }

    protected function article($category_code)
    {
        $category = Category::whereCode($category_code)->first();
        $article = Article::design($category->id ?? '')->take(6)->get();
        $article = $article->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });
        return $article;
    }
}